<template>
	<view class="edit">
		<u--form labelPosition="top" :model="addressForm" labelWidth="200" :rules="addressRules" ref="uForm"
			class="list">
			<u-form-item label="姓名" prop="hostname">
				<u--input v-model="addressForm.hostname" placeholder="请输入姓名">
				</u--input>
			</u-form-item>
			<u-form-item label="电话" prop="hostphone">
				<u--input v-model="addressForm.hostphone" placeholder="请输入电话">
				</u--input>
			</u-form-item>
			<u-form-item label="详细地址" prop="content">
				<u--input v-model="addressForm.content" placeholder="请输入街道门牌号">
				</u--input>
			</u-form-item>
			<!-- 			<u-form-item label="是否默认" prop="isDefault">
				<u-switch v-model="addressForm.isDefault" inactiveColor="#eee"></u-switch>
			</u-form-item> -->
			<u-form-item>
				<u-button type="primary" @click="onSubmit">提交</u-button>
			</u-form-item>
		</u--form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		GetStroage
	} from "@/utils/tool.js"
	export default {
		data() {
			return {
				addressForm: {
					hostname: "",
					hostphone: "",
					content: "",
					isDefault: false
				},
				addressRules: {
					hostname: [{
						required: true,
						message: '姓名是必填的',
						trigger: ['blur', 'change'],
					}, {
						min: 2,
						max: 8,
						message: '长度在2-8个字符之间',
						trigger: ['blur', 'change']
					}],
					hostphone: [{
						required: true,
						message: '电话是必填的',
						trigger: ['blur', 'change'],
					}, {
						validator: (rule, value, callback) => {
							return uni.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						trigger: ['change', 'blur']
					}],
					content: [{
						required: true,
						message: '地址是必填的',
						trigger: ['blur', 'change']
					}]
				},
				userInfo: {},
				msgList: {
					type: "success",
					message: "获取验证码成功"
				}
			}
		},
		onLoad() {
			this.userInfo = GetStroage("userInfo");
			// console.log(this.userInfo);
		},
		methods: {
			fillMsg(type, msg) { //提示消息的赋值
				this.msgList.type = type;
				this.msgList.message = msg;
				this.$refs.uToast.show({
					...this.msgList
				})
			},
			onSubmit() {
				this.$refs.uForm.validate().then(res => {
					uni.request({
						url: this.$e.Url + "/address/userAdd",
						data: {
							...this.addressForm,
							uid: this.userInfo.id
						},
						header: {
							"Content-Type": "application/json"
						},
						method: 'POST',
						success: (res) => {
							console.log("请求API接口【/user/userAdd】", res);
							if (res.statusCode == 200 && res.data) {
								this.fillMsg("success", "添加成功");
								setTimeout(() => {
									uni.navigateBack();
								}, 1000);
							}
						}
					})
				}).catch(errors => {
					this.fillMsg("error", "校验失败");
				})
			}
		}

	}
</script>

<style lang="scss">
	page {
		background-image: url('/static//userbackground.jpg');
		background-size: cover;
		background-repeat: no-repeat;
		background-position: center center;
	}

	.edit {
		padding: 30rpx;

		.list {}
	}
</style>
